﻿using System;

using HsTec.Mobile.Green.SqlServerDAL;

public partial class BaseData_EnergyCalcEdit : System.Web.UI.Page
{
    EnergyCalc ec = new EnergyCalc();

    public string sEditStatus = "1";
    private string sID = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
                if (sEditStatus == "2")
                {
                    sID = this.Request.QueryString["ID"].ToString().Trim();
                    EnergyCalc model = ec.GetModel(int.Parse(sID));
                    txtName.Text = model.EnergyName;
                    txtEnergyHot.Text = model.EnergyHot.ToString();
                    txtEnergyProportion.Text = model.EnergyProportion.ToString();
                }
                else
                {
                }
            }
            catch
            {
            }
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        //TODO:缺少重复性和有效性判断
        try
        {
            EnergyCalc model = new EnergyCalc();
            model.EnergyName = txtName.Text.Trim();
            model.EnergyHot = decimal.Parse(txtEnergyHot.Text.Trim());
            model.EnergyProportion = decimal.Parse(txtEnergyProportion.Text.Trim());

            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();

            if (sEditStatus == "1")
            {
                int iback = model.Add();
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                model.EC_ID = int.Parse(this.Request.QueryString["ID"].ToString().Trim());
                int iback = model.Update();
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");  //window.opener.location.reload();
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('操作失败!');</script>");
            return;
        }
    }
}
